// pages/order/order.js
const app = getApp()
import http from "../../utils/http"
import util from "../../utils/util"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    unread: 1,
    tabList: ['待接单', '进行中', '已完成', '售后订单'],
    orderState: 0,
    list: [],
    finish: false,
    loading: false,
    page: 1,
    active: 0,
    token: wx.getStorageSync('token')
  },
  orderBtnTap(e) {
    wx.requestSubscribeMessage({
      tmplIds: [app.globalData.templateconfig.skill_order_template, app.globalData.templateconfig.skill_sales_template],
      success: (res) => {
        console.log(res)
      },
      fail: (e) => {
        console.log(e)
      },
      complete: (res) => {
        if (e.detail.type == 1) {

          http.get('order/skillaccept', {
            id: e.detail.info.id
          }, false, false).then(res => {
            util.toast('抢单成功')
            this.reload()
            setTimeout(res => {
              util.skip('/pages/orderDetail/orderDetail?id=' + e.detail.info.id)
            }, 1000)
          }).catch(err => {
            let data = err.data
            if (data.code === 2) {
              util.toast(data.msg)
            } else if (data.code === 0) {
              util.toast(data.data)
            }
          })
        } else if (e.detail.type == 2) {
          http.get('order/skillaccept', {
            id: e.detail.info.id
          }, false, false).then(res => {
            util.toast('接单成功')
            this.reload()
            setTimeout(res => {
              util.skip('/pages/orderDetail/orderDetail?id=' + e.detail.info.id)
            }, 1000)
          }).catch(err => {
            let data = err.data
            if (data.code === 2) {

              if (e.detail.info.shop_id) {
                util.toast(data.msg)
              } else {
                util.toast(data.msg)
                setTimeout(() => {
                  util.skip('/service/earnestMoney/earnestMoney')
                }, 1000)
              }

            } else if (data.code === 0) {
              util.toast(data.data)
            }
          })
        }
      }
    })

  },
  orderTap(e) {
    util.skip('/pages/orderDetail/orderDetail?id=' + e.detail.id)
  },
  onChange(event) {
    this.setData({
      active: event.detail.index
    })
    this.reload()
  },
  reload() {
    this.setData({
      list: [],
      page: 1,
      finish: false
    })
    this.getList()
  },
  more() {
    this.setData({
      page: ++this.data.page
    })
    this.getList()
  },
  getList() {
    const {tabList, active} = this.data;
    if (!wx.getStorageSync('token')) return
    if (this.data.finish) {
      return
    }
    if (this.data.loading) {
      return
    }
    this.setData({
      loading: true
    })
    let data = {
      page: this.data.page,
    }
    if (tabList[active] === '接单池') {
      data['is_pool'] = 1
    } else if (tabList[active] === '待接单') {
      data['status'] = 1
    } else if (tabList[active] === '进行中') {
      data['not_finish'] = 1
    } else if (tabList[active] === '已完成') {
      data['finish'] = 1
    } else if (tabList[active] === '售后订单') {
      data['in_service'] = 1
    }
    http.get('order/skillorderlist', data).then(res => {
      if (res.data.length === 0) {
        this.setData({
          finish: true
        })
      }
      let arr = this.data.list.concat(res.data)
      this.setData({
        list: arr
      })

    }).finally(res => {
      this.setData({
        loading: false
      })
    })
  },


  getOrderGrabConfig() {
    let {
      tabList
    } = this.data;
    http.get('order/grabConfig', {}).then(res => {
      console.log(res)
      if (res.data && res.data.grab_open === 1) {
        tabList.unshift('接单池')
        this.setData({
          tabList
        })
      }
    }).finally(res => {
      
    })
    // service/order/grabConfig
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // wx.showModal({
    //   // title: '',
    //   title: '确认开始服务?',
    //   complete: (res) => {
    //     if (res.cancel) {
          
    //     }
    
    //     if (res.confirm) {
          
    //     }
    //   }
    // })
    this.setData({
      token: wx.getStorageSync('token')
    })
    this.reload()
    this.getOrderGrabConfig()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})